import SessionUtil, {
    JxbHttpClient
} from '../../data/session/session'
import SessionUserUtil from '../../data/session/sessionUser';
import SessionSellingServerUtil from '../../data/session/sessionSellingServer';
const Common = require('../../utils/common.js');
Page({
    data: {
        tabNum: true,
        userId: '',
        userName: '',
        passWord: '',

        SMCTime: 60,
        SMCTimes: true,
        SMCode_Lock: false,
        SMCode_Msg: '获取验证码',

        SMCode: '',
        token: '',
        openid: '',
        source: 0,
    },
    onLoad: function (options) {
        SessionUtil.getInstance().then(session => {
            let sesslinServer = session.getTheSellingServerSync(),
                agent = sesslinServer.getTheAgentSync(),
                applet = session.getTheAppletSync()
            this.setData({
                zhuimges: agent.logoUri ? agent.logoUri : applet.logoUri,
                zhuname: sesslinServer.showName ? sesslinServer.showName : applet.name,
                source: options.source ? options.source : this.data.source,
                is_account_register: !agent.customerRegistrationClosed,
                openid: session.openId
            });
            wx.setNavigationBarTitle({
                title: '登录'
            })
        })
    },
    loginTab: function (e) {
        var id = e.currentTarget.id;
        var page = this;
        if (id == 2) {
            page.setData({
                tabNum: false,
            })
        } else {
            page.setData({
                tabNum: true
            })
        }
    },
    // 更新用户名
    updateUserName: function (e) {
        this.setData({
            userName: e.detail.value
        })
    },
    // 更新密码
    updatePassWord: function (e) {
        this.setData({
            passWord: e.detail.value
        })
    },
    updateSMCode: function (e) {
        this.setData({
            SMCode: e.detail.value
        })
    },

    /**
     * 密码登录
     */
    onLogin: function (e) {
        var page = this;
        var userName = page.data.userName;
        var passWord = page.data.passWord;
        var openid = page.data.openid;
        var phonetest = /^0?1\d{10}$/;
        if (!userName) {
            Common.Alert('登录提示', '请输入手机号码');
            return false;
        } else if (!phonetest.test(userName)) {
            Common.Alert('登录提示', '请输入正确的手机号码');
            return false;
        }
        if (!passWord) {
            Common.Alert('登录提示', '请输入登录密码');
            return false;
        }
        wx.showLoading({
            title: '正在登录...',
        });
        (new JxbHttpClient).post({
            url: 'AppMaster/Login/login',
            data: {
                userName: userName,
                password: passWord,
                openid: openid,
            }
        }).then(jxbResp => {
            // 关闭加载
            wx.hideLoading();
            this.setData({
                token: jxbResp.data.token
            })
            // 设置token
            wx.setStorageSync('token', this.data.token);
            wx.removeStorageSync('signOut');
            wx.showToast({
                title: '登录成功',
                icon: 'success',
                duration: 2000
            })
            // 判断是否批发商
            Common.wholesale();
        }).catch(err => {
            // 关闭加载
            wx.hideLoading();
            Common.Alert('登录提示', err.msg);
        });
    },
    forgetPassword: function () {
        wx.navigateTo({
            url: '/pages/login/forget_password/forget_password'
        })
    },

    getSMCode: function (e) {
        var page = this;
        var userName = this.data.userName;
        var phonetest = /^0?1\d{10}$/;

        page.setData({
            SMCode_Lock: true
        });

        if (!userName) {
            Common.Alert('登录提示', '请输入手机号码');
            return false;
        } else if (!phonetest.test(userName)) {
            Common.Alert('登录提示', '请输入正确的手机号码');
            return false;
        }
        (new JxbHttpClient).get({
            url: '/AppMaster/Login/backPwdSendMessageCode',
            data: {
                phone: userName
            }
        }).then(jxbResp => {
            wx.showToast({
                title: '验证码已发送',
                icon: 'success',
                duration: 2000
            })
            // 倒计时显示
            this.SMCode_Msg();
        }).catch(err => {
            page.setData({
                SMCTime: 60,
                SMCode_Lock: false,
                SMCode_Msg: '获取验证码'
            })
            Common.Alert('错误提示', err.message);
        })
    },

    SMCode_Msg: function () {
        console.log('SMCode_Msg');
        var page = this;
        var SMCTimers;
        var SMCTime = page.data.SMCTime;
        var SMCode_Msg = page.data.SMCode_Msg;
        var SMCode_Lock = page.data.SMCode_Lock;
        if (SMCTime > 1) {
            SMCTime--;
            SMCode_Msg = '重新获取(' + SMCTime + ')';
            page.setData({
                SMCTime: SMCTime,
                SMCode_Msg: SMCode_Msg,
                SMCTimes: false,
            })
            // 定时器执行
            SMCTimers = setTimeout(function () {
                page.SMCode_Msg();
            }, 1000);

        } else {
            clearTimeout(SMCTimers);
            page.setData({
                SMCTime: 60,
                SMCode_Lock: false,
                SMCode_Msg: '获取验证码',
                SMCTimes: true,
            })
        }

    },
    /**
     * 清除缓存
     */
    getUserInfo: function () {
        SessionUtil.getInstance().then(session => {
            return Promise.all([SessionUserUtil.renewCache(), SessionSellingServerUtil.renewCache()])
        }).then(thenData => {
            wx.reLaunch({
                url: '/pages/my/index/index'
            })
        })
    },
    onLogin_SMC: function () {
        // 验证码登录
        var page = this;
        var userName = this.data.userName;
        var SMCode = this.data.SMCode;
        var phonetest = /^0?1\d{10}$/;

        if (!userName) {
            Common.Alert('登录提示', '请输入手机号码');
            return false;
        } else if (!phonetest.test(userName)) {
            Common.Alert('登录提示', '请输入正确的手机号码');
            return false;
        }

        if (!SMCode) {
            Common.Alert('登录提示', '请输入验证码');
            return false;
        }

        wx.showLoading({
            title: '正在登录...',
        });
        (new JxbHttpClient).post({
            url: '/AppMaster/Login/checkMessageCodeLogin',
            data: {
                phone: userName,
                code: SMCode,
            }
        }).then(jxbResp => {
            this.setData({
                token: jxbResp.data.token
            })
            wx.removeStorageSync('signOut');
            wx.hideLoading();
            wx.showToast({
                title: '登录成功',
                icon: 'success',
                duration: 2000
            })
            // 判断是否批发商
            Common.wholesale();
        }).catch(err => {
            wx.hideLoading();
            this.setData({
                SMCTime: 60,
                SMCode_Lock: false,
                SMCode_Msg: '获取验证码',
                SMCTimes: true,
            })
            Common.Alert('登录提示', err.message);
        })
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh()
    },
    /**
     * 去注册
     */
    register: function () {
        var consultantId = wx.getStorageSync('consultantId');
        if (consultantId > 0) {
            wx.navigateTo({
                url: '/pages/register/information1/index?salesmanVCardId=' + consultantId
            })
        } else {
            wx.navigateTo({
                url: '/pages/register/consultant/index'
            })

        }

    },
    /**
     * 去首页
     */
    index: function () {
        wx.switchTab({
            url: '/pages/index/index'
        })
    }
})